package com.hesicare.adapter.service.impl;

import com.hesicare.adapter.entity.HisDeptLevel;
import com.hesicare.adapter.service.IParseDeptService;
import com.hesicare.health.entity.SysDept;
import com.hesicare.health.service.ISysDeptService;
import com.hesicare.untils.httpUtils.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 55097
 */
@Service
public class ParseDeptServiceImpl implements IParseDeptService {

    protected final static Logger LOGGER = LoggerFactory.getLogger(ParseDeptServiceImpl.class);


    @Autowired
    private ISysDeptService ISysDeptService;

    @Value("{hesiUrl}")
    private String url;

    @Override
    public void parseDept(Object obList) {
        ArrayList<HisDeptLevel> list = (ArrayList<HisDeptLevel>) obList;
        list.stream().forEach(hisDeptLevel -> {
            String token = ISysDeptService.getToken();
            String request = null;
            try {
                SysDept sysDept = ISysDeptService.getSysDeptByHisCode(hisDeptLevel.getDepartmentCode());
                if (sysDept != null) {
                    return;
                }
                Map map = new HashMap<>();
                map.put("deptName", hisDeptLevel.getDepartmentName());
                map.put("glucosePeriodOrder", "1");
                map.put("glucoseRecordOrder", "desc");
                map.put("hisCode", hisDeptLevel.getDepartmentCode());
                map.put("orderNum", "0");
                map.put("leader", "0");
                map.put("parentId", 230);
                map.put("qaDeal", "0");
                map.put("qaLock", "0");
                map.put("qaWarn", "0");
                map.put("status", "0");
                map.put("wristbandType", "1");
                if (sysDept != null) {
                    map.put("deptId", sysDept.getDept_id());
                    request = HttpUtils.doPostWithJson(url + "/system/dept/edit", map, token);
                } else {
                    request = HttpUtils.doPostWithJson(url + "/system/dept/add", map, token);
                }
            } catch (NullPointerException e) {
                LOGGER.error("科室信息空指针" + request);
            } catch (Exception e) {
                LOGGER.error("科室信息维护错误" + request);
            }
        });
    }

    @Override
    //关闭查询结果无记录的科室权限
    public void updateDeptSelectStaus(String hisCode) {
        String token = ISysDeptService.getToken();
        SysDept sysDept = ISysDeptService.getSysDeptByHisCode(hisCode);
        Map map = new HashMap<>();
        map.put("phone", "1");
        map.put("deptName", sysDept.getDept_name());
        map.put("glucosePeriodOrder", "1");
        map.put("glucoseRecordOrder", "desc");
        map.put("hisCode", hisCode);
        map.put("orderNum", "0");
        map.put("leader", sysDept.getLeader());
        map.put("parentId", sysDept.getParent_id());
        map.put("qaDeal", "0");
        map.put("qaLock", "0");
        map.put("qaWarn", "0");
        map.put("status", sysDept.getStatus());
        map.put("wristbandType", "1");
        map.put("deptId", sysDept.getDept_id());
        String re = HttpUtils.doPostWithJson(url + "/system/dept/edit", map, token);
    }

    @Override
    public void updateLeaderDeptSelectStaus(String leader) {
        String token = ISysDeptService.getToken();
        SysDept sysDept = ISysDeptService.getSysDeptByLeader(leader);
        Map map = new HashMap<>();
        map.put("phone", "1");
        map.put("deptName", sysDept.getDept_name());
        map.put("glucosePeriodOrder", "1");
        map.put("glucoseRecordOrder", "desc");
        map.put("hisCode", sysDept.getHis_code());
        map.put("orderNum", "0");
        map.put("leader", sysDept.getLeader());
        map.put("parentId", sysDept.getParent_id());
        map.put("qaDeal", "0");
        map.put("qaLock", "0");
        map.put("qaWarn", "0");
        map.put("status", sysDept.getStatus());
        map.put("wristbandType", "1");
        map.put("deptId", sysDept.getDept_id());
        String re = HttpUtils.doPostWithJson(url + "/system/dept/edit", map, token);
    }
}
